<?php
namespace Admin\Form;

use Zend\Form\Form;
use Zend\Db\Adapter\AdapterInterface;

class MenuForm extends Form
{
	protected $dbAdapter;
    public function __construct(AdapterInterface $dbAdapter)
    {
		$this->setDbAdapter($dbAdapter);
        // we want to ignore the name passed
        parent::__construct('menu');

        $this->setAttribute('method', 'post');
        $this->add(array(
            'name' => 'id',
            'attributes' => array(
                'type'  => 'hidden',
            ),
        ));

       

        $this->add(array(
            'name' => 'tenchuyenmuc',
            'attributes' => array(
				'id' => 'tenchuyenmuc',
                'type'  => 'text',
				'required' => 'required',
            ),
            'options' => array(
                'label' => 'Tên chuyên mục',
            ),
        ));
		
		
		$this->add(array(
            'type' => 'select',
            'name' => 'theloai',
            'options' => array(
                'label' => 'Thể loại',
                'value_options' =>  $this->getOptionsForSelect(),
            ),
            'attributes' => array(
				'id' => 'theloai',
				'required' => 'required',
                'value' => '1' //set selected to '1'
            )
        ));

        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type'  => 'submit',
                'value' => 'Go',
                'id' => 'submitbutton',
            ),
        ));

    }
	
	public function getOptionsForSelect()
    {
        $dbAdapter = $this->getDbAdapter();
        $sql       = 'SELECT t0.id_theloai, t0.tentheloai FROM theloai t0 ORDER BY t0.id_theloai ASC';
        $statement = $dbAdapter->query($sql);
        $result    = $statement->execute();
 
        $selectData = array();
 		
        foreach ($result as $res) {
            $selectData[$res['id_theloai']] = $res['tentheloai'];
        }
 
        return $selectData;
    }
	public function setDbAdapter(AdapterInterface $dbAdapter)
    {
        $this->dbAdapter = $dbAdapter;

        return $this;
    }

    public function getDbAdapter()
    {
        return $this->dbAdapter;
    }
}
